package com.cyrusInnovation.test.record;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class PersonRecord {
	private String lastName="";
	private String firstName="";
	private Gender gender = Gender.UNKNOWN;
	private Date dateOfBirth;
	private String favoriteColor="";
	
	public PersonRecord(){
		
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getFavoriteColor() {
		return favoriteColor;
	}

	public void setFavoriteColor(String favoriteColor) {
		this.favoriteColor = favoriteColor;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public boolean equals(Object obj) {
		PersonRecord record;
		try {
			 record = (PersonRecord)obj;			
		} catch (Exception e) {
			return false;
		}
		if (getLastName().compareToIgnoreCase(record.getLastName())!=0){
			return false;
		}
		if (getFirstName().compareToIgnoreCase(record.getFirstName())!=0){
			return false;
		}
		if (getFavoriteColor().compareToIgnoreCase(record.getFavoriteColor())!=0){
			return false;
		}
		if (getGender()!=record.getGender()){
			return false;
		}

		if (!isTheSameDate(getDateOfBirth(), record.getDateOfBirth())) {
			return false;
		}
		
		return true;
	}
	
	private boolean isTheSameDate(Date date1, Date date2){
		if ((date1==null)||(date2==null)){
			if ((date1==null)&&(date2==null)) {
				return true;
			}
			return false;
		}
		
		Calendar calendar1 = new GregorianCalendar();
		Calendar calendar2 = new GregorianCalendar();
		calendar1.setTime(date1);
		calendar2.setTime(date2);
		
		if (calendar1.get(Calendar.YEAR)!=calendar2.get(Calendar.YEAR)){
			return false;
		}
		if (calendar1.get(Calendar.DAY_OF_YEAR)!=calendar2.get(Calendar.DAY_OF_YEAR)){
			return false;
		}		
		return true;
	} 
}
